#***************************************************************************
#                                  _   _ ____  _
#  Project                     ___| | | |  _ \| |
#                             / __| | | | |_) | |
#                            | (__| |_| |  _ <| |___
#                             \___|\___/|_| \_\_____|
#
# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at https://curl.se/docs/copyright.html.
#
# You may opt to use, copy, modify, merge, publish, distribute and/or sell
# copies of the Software, and permit persons to whom the Software is
# furnished to do so, under the terms of the COPYING file.
#
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# KIND, either express or implied.
#
# SPDX-License-Identifier: curl
#
###########################################################################

# 添加一个自定义目标 curl-examples，该目标不对应实际的构建产物，主要用于组织其他相关目标
add_custom_target(curl-examples)

# 转换 Makefile.inc 文件为 CMake 可识别的文件
# 并获取 'check_PROGRAMS' 变量，该变量包含需要构建的示例程序名称列表
curl_transform_makefile_inc("Makefile.inc" "${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
# 包含转换后的 Makefile.inc.cmake 文件，以便使用其中定义的变量
include("${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")

# 遍历 check_PROGRAMS 列表中的每个示例程序名称
foreach(_target IN LISTS check_PROGRAMS)
  # 为每个示例程序生成唯一的目标名称
  set(_target_name "curl-example-${_target}")
  # 添加可执行文件目标，EXCLUDE_FROM_ALL 表示该目标不会被默认构建
  add_executable(${_target_name} EXCLUDE_FROM_ALL "${_target}.c")
  # 将当前示例程序目标添加为 curl-examples 目标的依赖项
  # 这样在构建 curl-examples 目标时，会自动构建这些示例程序
  add_dependencies(curl-examples ${_target_name})
  # 将选定的 libcurl 库和其他必要的库链接到当前示例程序
  target_link_libraries(${_target_name} ${LIB_SELECTED} ${CURL_LIBS})
  # 为当前示例程序添加私有编译定义
  # CURL_NO_OLDIES 表示不使用旧版的 API
  # 若使用 MSVC 编译器，添加 _CRT_SECURE_NO_DEPRECATE 定义以禁用 CRT 安全警告
  target_compile_definitions(${_target_name} PRIVATE "CURL_NO_OLDIES" "$<$<BOOL:MSVC>:_CRT_SECURE_NO_DEPRECATE>")
  # 若选择链接静态库，并且是 Windows 平台
  if(LIB_SELECTED STREQUAL LIB_STATIC AND WIN32)
    # 为当前示例程序添加 CURL_STATICLIB 编译定义，表明使用静态链接
    set_property(TARGET ${_target_name} APPEND PROPERTY COMPILE_DEFINITIONS "CURL_STATICLIB")
  endif()
  # 设置当前示例程序目标的属性
  set_target_properties(${_target_name} PROPERTIES
    OUTPUT_NAME "${_target}"  # 设置生成的可执行文件的名称
    UNITY_BUILD OFF  # 禁用统一构建模式
    PROJECT_LABEL "Example ${_target}"  # 设置在 IDE 中显示的项目标签
  )
endforeach()
